package com.onpositive.nlp.sequence;

import java.util.HashMap;
import java.util.HashSet;

import com.onpositive.wiki3.db.ITitleMapper;

public class WikiLookup implements ILookup {

	public WikiLookup(String original, ITitleMapper mapper) {
		super();
		this.original = original;
		this.mapper = mapper;
	}

	final String original;
	final ITitleMapper mapper;

	static HashMap<String, Integer> ms = new HashMap<String, Integer>();
	static HashSet<String> neverReturn = new HashSet<String>();

	static {
		neverReturn.add("stubs");
		neverReturn.add("");
		neverReturn.add("is");
		neverReturn.add("an");
		neverReturn.add("or");
		neverReturn.add("a");
		neverReturn.add("it");
		neverReturn.add("its");
		neverReturn.add("is_a");
		neverReturn.add("of");
		neverReturn.add("and");
		neverReturn.add("as");
		neverReturn.add("as_a");
		neverReturn.add("the");
		neverReturn.add("used");
		neverReturn.add("for");
		neverReturn.add("which");
		neverReturn.add("also");
	}

	@Override
	public int get(String s) {
		if (neverReturn.contains(s.toLowerCase())) {
			return -1;
		}
		String[] split = s.split("_");
		if (split.length > 4) {
			return -1;
		}
		if (s.equals(original)) {
			return -1;
		}
		if (ms.containsKey(s)) {
			return ms.get(s);
		}
		int id = mapper.getId(s);
		if (id == -1) {
			int[] fuzzyMatches = mapper.getFuzzyMatches(s);
			if (fuzzyMatches.length > 0) {
				id = fuzzyMatches[0];
			}
		}
		ms.put(s, id);
		return id;
	}
}
